package thread.sync;

import thread.invariantframework.Invariant;
import thread.invariantframework.InvariantWatcher;
import thread.invariantframework.state.InvariantFailure;
import thread.invariantframework.state.InvariantOK;
import thread.invariantframework.state.InvariantState;

public class SynchronizedResource implements Invariant {

	private int i;

	public synchronized void next() {
		i++;
		i++;
	}

	public synchronized int getValue() {
		return i;
	}

	// Not synchronized so it can run at
	// any time and thus be a genuine test:
	public InvariantState invariant() {
		int val = getValue();
		if (val % 2 == 0) {
			return new InvariantOK();
		} else {
			return new InvariantFailure(new Integer(val));
		}
	}

	public static void main(String[] args) {
		SynchronizedResource r = new SynchronizedResource();
		new InvariantWatcher(r, 5000);
		while (true) {
			r.next();
		}
	}
}